package love.yuanmeng.guetcoalsystem.cotroller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import love.yuanmeng.guetcoalsystem.configuration.AlipayConfig;
import love.yuanmeng.guetcoalsystem.pojo.Bond;
import love.yuanmeng.guetcoalsystem.pojo.BondEnterpriseMap;
import love.yuanmeng.guetcoalsystem.pojo.User;
import love.yuanmeng.guetcoalsystem.service.BondService;
import love.yuanmeng.guetcoalsystem.util.JsonXMLUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletResponse;

/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:Lishao
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/10/13 19:30
 */
@Controller
@CrossOrigin
@RequestMapping("bond")
public class bondController {
    @Autowired
    private BondService bondservice;

    @GetMapping("addbond")
    @ResponseBody
    public String addbond(String id,String price) {
            return bondservice.addbond(id,price);
    }

    @GetMapping("viewbond")
    @ResponseBody
    public String viewbond(@RequestParam("id") String id) {
        BondEnterpriseMap bo = bondservice.selectbond(id);
        System.out.println("dddd" + JsonXMLUtils.obj2json(bo));
        return JsonXMLUtils.obj2json(bo);
    }

    @GetMapping("alipay")
    @ResponseBody
    public String alipay(String oid, String oname, String price) {
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //设置请求参数
        System.out.println(oname+"oname");
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        if("订单支付".equals(oname)){
            alipayRequest.setReturnUrl("http://localhost:8080/#/OrderPaySuccess");
        }
        else {
            alipayRequest.setReturnUrl(AlipayConfig.return_url);
        }
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = oid;
        //付款金额，必填
        String total_amount = price;
        //订单名称，必填
        String subject = oname;
        //商品描述，可空
        String body = null;

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
        //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
        //		+ "\"total_amount\":\""+ total_amount +"\","
        //		+ "\"subject\":\""+ subject +"\","
        //		+ "\"body\":\""+ body +"\","
        //		+ "\"timeout_express\":\"10m\","
        //		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

        //请求
        String result=null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.println(result);
         return result;
        //输出

    }

}
